package com.algorithm.lzr.array;

import com.sun.tools.javac.util.Assert;

/**
 * @Title: MaxArea
 * @Package com.algorithm.lzr.array
 * @Author liu.zr 2019-07-18 00:07
 * @Description: <求一维数组组成的(x,y)形成的最大平方面积，数组长度大于2>
 */
public class MaxArea {

    public static void main(String[] args) {
        int[] array = new int[]{1, 8, 3, 4, 6, 2, 9, 4, 7};
        Assert.check(maxArea(array) == 49);
    }

    /**
     * @Title: maxArea
     * @Description: <双指针法 时间复杂度O(N) 空间复杂度O(1)>
     * @Author liu.zr 2019-07-18 10:59
     * @Param: [array]
     * @Return: int
     */
    public static int maxArea(int[] array) {
        // x轴 = right - left
        // y轴 = Math.min(array[left], array[right]
        int maxArea = 0, left = 0, right = array.length - 1;
        while (left < right) {
            maxArea = Math.max(maxArea, Math.min(array[left], array[right]) * (right - left));
            if (array[left] < array[right]) left++;
            else right--;
        }
        return maxArea;
    }

}
